//
//  AllTabCell.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/12/12.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AllTabCell.h"
#import "ATabCCell.h"

@interface AllTabCell () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewH;

@property (nonatomic, strong) NSArray *dataSource;

@end

@implementation AllTabCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    self.dataSource = @[];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"ATabCCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ATabCCell"];
    
}

- (void)setDataSource:(NSArray *)dataSource {
    
    _dataSource = dataSource;
    
    CGFloat aLineHeight = ((kScreenWidth - 32) - 40) / 5 + 20;
    
    CGFloat height = ceil(dataSource.count / 5.0f) * aLineHeight + (ceil(dataSource.count / 5.0f) - 1) * 10 + 32;
    
    self.collectionViewH.constant = height;
}

#pragma mark -UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    ATabCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ATabCCell" forIndexPath:indexPath];
    cell.nameLabel.text = self.dataSource[indexPath.row];
    
    return cell;
}

#pragma mark -UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([_delegate respondsToSelector:@selector(selectTab:tag:)]) {
        [_delegate selectTab:self tag:indexPath.row];
    }
    
}

#pragma mark -UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat width = ((kScreenWidth - 32) - 40) / 5;
    
    return CGSizeMake(width, width + 20);
}

@end
